package com.ggb.smartstudy.service.util;

import com.ggb.smartstudy.entity.Permission;
import com.ggb.smartstudy.entity.Role;
import com.ggb.smartstudy.entity.User;
import com.ggb.smartstudy.mapper.*;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author gefangjie
 */
@Service
public class CustomUserDetailsService implements UserDetailsService {

    private final UserMapper userMapper;
    private final UserRoleMapper userRoleMapper;
    private final RoleMapper roleMapper;
    private final RolePermissionMapper rolePermissionMapper;
    private final PermissionMapper permissionMapper;

    public CustomUserDetailsService(UserMapper userMapper, UserRoleMapper userRoleMapper, RoleMapper roleMapper, RolePermissionMapper rolePermissionMapper, PermissionMapper permissionMapper) {
        this.userMapper = userMapper;
        this.userRoleMapper = userRoleMapper;
        this.roleMapper = roleMapper;
        this.rolePermissionMapper = rolePermissionMapper;
        this.permissionMapper = permissionMapper;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 通过用户名查找用户
        User user = userMapper.getUserByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException("不存在的用户！！！");
        }

        // 获取用户的角色id列表
        List<Integer> roleIds = userRoleMapper.getRoleIdByUserId(user.getId());
        // 创建一个权限列表
        List<GrantedAuthority> authorities = new ArrayList<>();

        // 遍历角色id列表
        for (Integer roleId : roleIds) {
            // 通过角色id获取角色信息
            Role role = roleMapper.getRoleById(roleId);
            System.out.println("Role Name: " + role);
            if (role != null && role.getRoleName() != null && !role.getRoleName().isEmpty()) {
                System.out.println("Role Name: " + role.getRoleName());
                authorities.add(new SimpleGrantedAuthority(role.getRoleName()));
            } else {
                throw new IllegalArgumentException("角色名不可以为空或不存在！");
            }

            // 获取当前角色id的权限id列表
            List<Integer> permissionIds = rolePermissionMapper.getPermissionIdByRoleId(roleId);
            for (Integer permissionId : permissionIds) {
                Permission permission = permissionMapper.getPermissionById(permissionId);
                if (permission != null && permission.getPermissionName() != null && !permission.getPermissionName().isEmpty()) {
                    authorities.add(new SimpleGrantedAuthority(permission.getPermissionName()));
                } else {
                    throw new IllegalArgumentException("Permission name must not be null or empty");
                }
            }
        }

        return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), authorities);
    }

}
